'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.renderTable = exports.exportTable = undefined;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _nanoid = require('nanoid');

var _nanoid2 = _interopRequireDefault(_nanoid);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var exportTable = exports.exportTable = function exportTable(table, fileName) {
  var html = '<html><head><meta charset=\'utf-8\' /></head><body>' + table + '</body></html>';
  var aTag = document.createElement('a');
  var blob = new Blob([html], { type: 'application/vnd.ms-excel' });
  aTag.download = (fileName ? fileName : new Date().getTime()) + '.xls';
  aTag.href = URL.createObjectURL(blob);
  aTag.click();
  URL.revokeObjectURL(blob);
};

var renderTable = exports.renderTable = function renderTable() {
  var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : { header: [], body: [] },
      _ref$header = _ref.header,
      header = _ref$header === undefined ? [] : _ref$header,
      _ref$body = _ref.body,
      body = _ref$body === undefined ? [] : _ref$body;

  if (header.length === 0 || body.length === 0) return;
  var tHeader = header.map(function (item) {
    return _react2.default.createElement(
      'th',
      { key: (0, _nanoid2.default)() },
      item
    );
  });
  var tBody = body.map(function (val) {
    return _react2.default.createElement(
      'tr',
      { key: (0, _nanoid2.default)() },
      val.map(function (ele) {
        return _react2.default.createElement(
          'td',
          { key: (0, _nanoid2.default)() },
          ele
        );
      })
    );
  });
  return _react2.default.createElement(
    'table',
    { border: '1', align: 'center' },
    _react2.default.createElement(
      'thead',
      null,
      _react2.default.createElement(
        'tr',
        null,
        tHeader
      )
    ),
    _react2.default.createElement(
      'tbody',
      null,
      tBody
    )
  );
};